package test;


import cn.ift8.domain.Customer;
import cn.ift8.domain.Thing;
import cn.ift8.domain.ThingOrder;
import cn.ift8.service.CustomerService;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.Test;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by IFT8
 * on 2015/8/24.
 */
public class T {
    private static SessionFactory sessionFactory;

    static {
        Configuration configuration = new Configuration();
        configuration.configure("config/hibernate/hibernate.cfg.xml");
        sessionFactory = configuration.buildSessionFactory();
    }

    @Test
    public void creatTable() {
        Session session = sessionFactory.getCurrentSession();
        session.close();
    }

    private Customer makeCustomer() {
        Thing thing1 = new Thing("t1", 1, 10.00, "t11");
        Thing thing2 = new Thing("t2", 1, 10.00, "t22");
        Thing thing3 = new Thing("t3", 1, 10.00, "t33");
        Set<Thing> orderThings = new HashSet<Thing>();
        orderThings.add(thing1);
        orderThings.add(thing2);
        Set<Thing> buyCart = new HashSet<Thing>();
        buyCart.add(thing2);
        buyCart.add(thing3);
        Set<ThingOrder> orders = new HashSet<ThingOrder>();
        ThingOrder order = new ThingOrder(new Date(System.currentTimeMillis()), orderThings);
        orders.add(order);
        return new Customer("L", "zxc", buyCart, orders);
    }

    @Test
    public void prop() {
        Customer customer = makeCustomer();
        Session session = sessionFactory.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        session.save(customer);
        transaction.commit();
    }

    @Test
    public void show() {
        Session session = sessionFactory.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        Customer customer = (Customer) session.get(Customer.class, 1L);
        System.out.println(customer.toString());
        transaction.commit();
    }

    @Test
    public void testSessionFactory(){
        ApplicationContext context=new ClassPathXmlApplicationContext("config/spring/applicationContext.xml");
        context.getBean("sessionFactory");
    }

    @Test
    public void testService() {
//        Customer customer = makeCustomer();

        ApplicationContext context=new ClassPathXmlApplicationContext("config/spring/applicationContext.xml");
        CustomerService customerService = (CustomerService) context.getBean("customerService");
//        customerService.save(customer);

        /*   测试时候需关闭懒加载   */
        Session currentSession = sessionFactory.getCurrentSession();
        Transaction transaction = currentSession.beginTransaction();
        Customer customer1 = customerService.getById(2L);
        customer1.setCname("W");
        customerService.save(customer1);
        transaction.commit();
    }
}
